package com.quectel.business.dms.application.access.accessor.ieg.impl;

import com.quectel.business.dms.application.ApplicationAccessConf;
import com.quectel.business.dms.application.access.accessor.ieg.AbstractIntelligentEntranceGuardAccessor;
import com.quectel.business.dms.application.access.accessor.ieg.model.DeviceFaceSendCmd;
import com.quectel.business.dms.application.access.accessor.ieg.model.DeviceQrCodeGenCmd;
import com.quectel.business.dms.application.access.basemodel.ApplicationOriginDevice;
import com.quectel.core.module.application.dto.ApplicationDto;
import com.quectel.core.module.device.dto.DeviceCommandTemplateDto;
import com.quectel.core.module.device.dto.DeviceDto;
import com.quectel.core.module.device.service.DeviceTypeService;
import com.quectel.sdk.hik8700.Hik8700ClientBuilder;
import com.quectel.sdk.hik8700.entranceguard.EntranceGuardClient;
import com.quectel.sdk.hik8700.entranceguard.model.SyncGuardResp;
import com.quectel.sdk.hikisc.HikISCRequester;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author rananxin
 * @date 2021/11/29 5:33 下午
 */
@Component
@Slf4j
public class Hik8700EntranceGuardAccessorImpl extends AbstractIntelligentEntranceGuardAccessor {

    @DubboReference
    private DeviceTypeService deviceTypeService;

    public List<AbstractIntelligentEntranceGuardAccessor.AbilityEnum> abilities() {
        return Lists.newArrayList();
    }

    @Override
    public boolean openDoor(List<DeviceDto> doors) {

        return false;
    }

    @Override
    public void authPerson(DeviceFaceSendCmd deviceInfo) {

    }

    @Override
    public String genQrCode(DeviceQrCodeGenCmd deviceQrCodeGenCmd) {
        return null;
    }

    @Override
    public boolean syncOrRegisterDevices(ApplicationDto application, List<ApplicationOriginDevice> devices) {

        final Date currentDate = new Date();

        ApplicationAccessConf.TemplateOfHik8700Conf templateOfHik8700Conf = ApplicationAccessConf
                .parseExtStr(ApplicationAccessConf.TemplateOfHik8700Conf.class, application.getAccessConf());

        try {
            EntranceGuardClient entranceGuardClient = Hik8700ClientBuilder.builder()
                    .key(templateOfHik8700Conf.getKey())
                    .secret(templateOfHik8700Conf.getSecret())
                    .host(templateOfHik8700Conf.getHost())
                    .build(EntranceGuardClient.class);

            List<SyncGuardResp> syncCameraResps = entranceGuardClient.drawResources();
            List<DeviceDto> deviceDtos = syncCameraResps.parallelStream()
                    .filter(o -> deviceService.selectByUniqueId(o.getDeviceUuid(), application.getTenantId()) == null)
                    .map(o -> {
                        DeviceDto deviceDto = new DeviceDto();
                        deviceDto.setUniqueId(o.getDoorUuid());
                        deviceDto.setImei(o.getDoorUuid());
                        deviceDto.setName(o.getDoorName());
                        deviceDto.setCreateTime(currentDate);

                        fill(deviceDto, application);
                        return deviceDto;
                    }).collect(Collectors.toList());

            deviceService.saveBatch(deviceDtos);
            return true;
        } catch (Exception e) {
            log.error("海康8700 门禁同步 syncOrRegisterDevices 异常", e);
        }
        return false;
    }

    @Override
    public boolean subscribeEvents(ApplicationDto applicationDto) {
        ApplicationAccessConf.TemplateOfHikIscConf templateOfHikIscConf = ApplicationAccessConf.parseExtStr(ApplicationAccessConf.TemplateOfHikIscConf.class, applicationDto.getAccessConf());

        String subPath = "/api/eventService/v1/eventSubscriptionByEventTypes";
        HikISCRequester instance = HikISCRequester.getInstance(templateOfHikIscConf.getAk()
                , templateOfHikIscConf.getSk(), templateOfHikIscConf.getHost());

//        Map<String, List<Integer>> codeArr = HikIscConstants.EventEnum.getCodeArr();
//        for (String path : codeArr.keySet()) {
//            String response = instance.post(subPath, new HashMap<String, Object>() {{
//                put("eventTypes", codeArr.get(path).stream().toArray(Integer[]::new));
//                put("eventDest", templateOfHikIscConf.getUrl());
//            }});
//        }
        return true;
    }

    @Override
    public boolean initApplication(ApplicationDto application) {
        syncOrRegisterDevices(application, null);
        subscribeEvents(application);
        return true;
    }

    @Override
    public boolean sendCmd(DeviceDto deviceDto, DeviceCommandTemplateDto deviceCommandTemplateDto, Map<String, Object> params, Long cmdId) {
        return true;
    }
}
